package priv.wangy.micro.common.constant;

import cn.hutool.core.util.StrUtil;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangjq
 * @since 2024-05-13
 */
public class BaseDictionary {

    public static final String PADDING_CODE = "RxsjWcDCSyNmKLy5D4OjKwJm8onwRhE3uSz5RCjZw3QdzNPvagC2HiuWIoqiKPQU";
    protected static final Map<Class<? extends BaseDictionary>, String> FILED_NAME = new HashMap<>();

    protected static String getListRspnFieldName(Class<? extends BaseDictionary> clazz) {
        if (FILED_NAME.containsKey(clazz)) {
            return FILED_NAME.get(clazz);
        }
        String result = null;
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (Modifier.isStatic(field.getModifiers()) && String.class.isAssignableFrom(field.getType())
                    && "FIELD_NAME".equals(field.getName())) {
                field.setAccessible(true);
                try {
                    result = (String) field.get(null);
                    break;
                } catch (IllegalAccessException ignore) {
                }
            }
        }
        if (!StringUtils.hasLength(result)) {
            result = StrUtil.toUnderlineCase(clazz.getSimpleName()).toUpperCase();
        }
        FILED_NAME.put(clazz, result);
        return result;
    }

}
